package com.ftg.learn.chapter04.linetable;

import java.util.Queue;

/**
 * 队列
 * fifo
 * first in first out
 *
 * @author 布衣
 */
public class QueueForMe {

    private int[] arr;

    public QueueForMe() {
        this.arr = new int[10];
    }

    private int size;

    /**
     * 入队
     */
    public void add(int val) {
//        扩容
        if (size == arr.length) {
            int[] newArr = new int[arr.length * 2];
            System.arraycopy(arr, 0, newArr, 0, arr.length);
            arr = newArr;
        }
        arr[size++] = val;
    }

    public int poll() {
        int ret = arr[0];
        System.arraycopy(arr, 1, arr, 0, size - 1);
        size--;
        return ret;
    }

    public static void main(String[] args) {
        QueueForMe queue = new QueueForMe();
        for (int i = 0; i < 10; i++) {
            queue.add(i + 1);
        }

        while(queue.size > 1){
            queue.add(queue.poll());
            queue.add(queue.poll());
            queue.poll();
        }

        System.out.println(queue.poll());

    }

}
